﻿using mozhi.smarterp.Foundation.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Volo.Abp;
using Volo.Abp.Domain.Entities;

namespace mozhi.smarterp.Foundation.OperationLogTemplates
{
    public class OperationLogTemplate : AggregateRoot<Guid>
    {
        public string Code { private set; get; }

        /// <summary>
        /// 名称（对应的应该是具体的操作对象：如果供应商，客户，凭证等）
        /// </summary>
        public string Name { private set; get; }

        public string Comment { private set; get; }

        public List<OperationLogTemplateItem> Items { private set; get; }

        private OperationLogTemplate() { }

        public OperationLogTemplate(Guid id, string code, string name, string comment)
        {
            Id = id;
            Code = code;
            Name = name;
            Comment = comment;
            Items = new List<OperationLogTemplateItem>();

            Validate();
        }

        public void Update(string code , string name , string comment)
        {
            Code = code;
            Name = name;
            Comment = comment;

            Validate();
        }

        public void UpdateItems(List<OperationLogTemplateItem> items)
        {
            if (items == null || items.Count == 0)
                Items.Clear();


            items.ForEach(item => AddOrUpdate(item));

            var itemIds = items.Select(item => item.Id);

            var deletedItems = Items.Where(x=>!itemIds.Contains(x.Id)).ToList();

            if (deletedItems.Count > 0)
                deletedItems.ForEach(item => Items.Remove(item));
        }

        public void AddItem(OperationLogTemplateItem item)
        {
            if (Items.Exists(x => x.FieldName == item.FieldName))
                throw new UserFriendlyException($"字段：{item.FieldName}已存在", FoundationErrorCodes.DuplicateKey);

            Items.Add(item);
        }

        public void UpdateItem(OperationLogTemplateItem item)
        {
            var match = Items.FirstOrDefault(x => x.Id == item.Id);

            if (match == null)
                throw new UserFriendlyException($"字段：{item.FieldName}已存在", FoundationErrorCodes.NotExist);

            match.Update(item.FieldName, item.FieldCaption, item.FieldType);
        }

        public void AddOrUpdate(OperationLogTemplateItem item)
        {
            if (Items.Exists(x => x.Id == item.Id))
                UpdateItem(item);
            else
                AddItem(item);
        }

        #region
        private void Validate()
        {
            Check.NotNull(Code, nameof(Code));
            Check.NotNull(Name, nameof(Name));
        }
        #endregion
    }

    public class OperationLogTemplateItem : Entity<Guid>
    {
        public Guid OperationLogTemplateId{ private set; get; }

        public string FieldName { private set; get; }

        public string FieldCaption { private set; get; }

        /// <summary>
        /// 数据类型 1用户Id , 2供应商Id ,3 科目 4 文件 , 5会计准则 6币别
        /// </summary>
        public LogFieldType FieldType { private set; get; }

        private OperationLogTemplateItem() { }

        public OperationLogTemplateItem(Guid id, Guid parentId, string fieldName, string fieldCaption, LogFieldType fieldType)
        {
            Id = id;
            OperationLogTemplateId = parentId;
            FieldName = fieldName;
            FieldCaption = fieldCaption;
            FieldType = fieldType;

            Validate();
        }

        public void Update(string fieldName, string fieldCaption, LogFieldType fieldType)
        {
            FieldName = fieldName;
            FieldCaption = fieldCaption;
            FieldType = fieldType;
            Validate();
        }

        #region
        private void Validate()
        {
            Check.NotNull(FieldName , nameof(FieldName));
            Check.NotNull(FieldType , nameof(FieldType));
            Check.NotNull(FieldCaption, nameof(FieldCaption));
        }
        #endregion
    }
}
